<template>
	<view class="u-wrap">
		<u-search @change="searchBtn"  v-model="parms.keywords" margin="10px" style="flex-grow: 1;" bg-color="#f2f2f2" :show-action="true" action-text="搜索"
			:animation="true"></u-search>
		<view class="tab-sticky">
			<u-tabs active-color="#4BC26A" name="categoryName" :list="tabList" :is-scroll="true" v-model="current"
				@change="change"></u-tabs>
		</view>
		<!-- 瀑布流 -->
		<view class="">
			<u-waterfall v-model="flowList" ref="uWaterfall1">
				<template v-slot:left="{leftList}">
					<view class="demo-warter" v-for="(item, index) in leftList" :key="index">
						<!-- 警告：微信小程序中需要hx2.8.11版本才支持在template中结合其他组件，比如下方的lazy-load组件 -->
						<u-lazy-load @click="toFind(item)" threshold="-450" border-radius="10" :image="item.image" :index="index">
						</u-lazy-load>
						<view class="demo-title">
							{{item.title}}
						</view>
						<view class="demo-shop">
							{{item.introduce}}
						</view>
						<view class="demo-tag">
							<view v-if="item.type == '0'" class="demo-tag-owner">
								失物
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								拾物
							</view>
						</view>
					</view>
				</template>
				<template v-slot:right="{rightList}">
					<view class="demo-warter" v-for="(item, index) in rightList" :key="index">
						<u-lazy-load threshold="-450" border-radius="10" :image="item.image" :index="index">
						</u-lazy-load>
						<view class="demo-title">
							{{item.title}}
						</view>
						<view class="demo-shop">
							{{item.introduce}}
						</view>
						<view class="demo-tag">
							<view v-if="item.type == '0'" class="demo-tag-owner">
								失物
							</view>
							<view style="margin-left: 0;" v-else class="demo-tag-text">
								拾物
							</view>
						</view>
					</view>
				</template>
			</u-waterfall>
			<u-loadmore bg-color="rgb(240, 240, 240)" :status="loadStatus" @loadmore="loadMore"></u-loadmore>
		</view>
	</view>
</template>

<script setup>
	import { onReady,onReachBottom } from '@dcloudio/uni-app';
import {
		ref,
		reactive 
	} from 'vue';
	import {getWxCateListApi,getFindAndClimListApi} from '../../src/api/find.js'
	const tabList = ref([])
	const current = ref(0)
	//瀑布流
	const flowList = ref([])
	//获取分类数据
	const getWxCateList = async()=>{
		let res = await getWxCateListApi()
		if(res && res.code == 200){
			console.log(res)
			tabList.value = res.data;
			//tabs第一个添加全部
			tabList.value.unshift({
				categoryId:"",
				categoryName:"全部",
				orderNum:0
			})
		}
	}
	//列表数据加载
	const uWaterfall1 = ref()
	const loadStatus = ref('loadmore')
	const pages = ref(0)
	const parms = reactive({
		currentPage:1,
		pageSize:10,
		keywords:'',
		categoryId:'',
		type:'0'
	})
	const getIndexList = async()=>{
		let res = await getFindAndClimListApi(parms)
		if(res && res.code == 200){
			console.log(res)
			flowList.value = flowList.value.concat(res.data.records)
			pages.value = res.data.pages
		}
	}
	//加载更多
	const loadMore = ()=>{
		console.log('点击加载更多')
		//如果当前页大于总页数，说明没有数据了
		if(parms.currentPage >= pages.value){
			loadStatus.value = 'nomore'
			return;
		}
		loadStatus.value = 'loading'
		//页数加1
		parms.currentPage = ++parms.currentPage;
		//获取数据
		getIndexList()
	}
	//触底加载
	onReachBottom(()=>{
		console.log('触底加载')
		//如果当前页大于总页数，说明没有数据了
		if(parms.currentPage >= pages.value){
			loadStatus.value = 'nomore'
			return;
		}
		loadStatus.value = 'loading'
		//页数加1
		parms.currentPage = ++parms.currentPage;
		//获取数据
		getIndexList()
	})
	//tabs点击事件
	const change = (e)=>{
		// console.log(tabList.value[e])
		// console.log(tabList.value[e].categoryId)
		parms.categoryId = tabList.value[e].categoryId
		//清空瀑布流数据
		uWaterfall1.value.clear()
		parms.currentPage = 1;
		//获取数据
		getIndexList()
	}
	//搜索
	const searchBtn = ()=>{
		//清空瀑布流数据
		uWaterfall1.value.clear()
		parms.currentPage = 1;
		//获取数据
		getIndexList()
	}
	onReady(()=>{
		//分类
		getWxCateList()
		//列表
		getIndexList()
	})
	//跳转
	const toFind =(item)=>{
	
		uni.navigateTo({
			url:'../../pages/find_detail/find_detail?goods='+JSON.stringify(item)
		})
	}
</script>

<style lang="scss">
	.demo-warter {
		border-radius: 8px;
		margin: 5px;
		background-color: #ffffff;
		padding: 8px;
		position: relative;
	}

	.u-close {
		position: absolute;
		top: 32rpx;
		right: 32rpx;
	}

	.demo-image {
		width: 100%;
		border-radius: 4px;
	}

	.demo-title {
		font-size: 30rpx;
		margin-top: 5px;
		color: $u-main-color;
	}

	.demo-tag {
		display: flex;
		margin-top: 5px;
	}

	.demo-tag-owner {
		background-color: $u-type-error;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		padding: 4rpx 14rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 1;
	}

	.demo-tag-text {
		border: 1px solid $u-type-primary;
		color: $u-type-primary;
		margin-left: 10px;
		border-radius: 50rpx;
		line-height: 1;
		padding: 4rpx 14rpx;
		display: flex;
		align-items: center;
		border-radius: 50rpx;
		font-size: 20rpx;
	}

	.demo-price {
		font-size: 30rpx;
		color: $u-type-error;
		margin-top: 5px;
	}

	.demo-shop {
		font-size: 22rpx;
		color: $u-tips-color;
		margin-top: 5px;
	}

	.tab-sticky {
		position: sticky;
		z-index: 99;
		top: 0;
		left: 0;
	}
	/*去掉tabs选项卡滚动条*/
		scroll-view ::v-deep ::-webkit-scrollbar {
		   width: 0;
		   height: 0;
		   color: transparent
		}
</style>
